<?php

    class ProfileController extends Zend_Controller_Action
    {

        public function init()
        {
            /* Initialize action controller here */
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $this->user_id = $x->read()->user_id;
                $this->loggedin_full_name = $x->read()->user_first_name . " " . $x->read()->user_last_name;
            }
        }

        public function indexAction()
        {
            $this->view->bodyClass = 'profile-events';
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $user_id = $x->read()->user_id;
                $custom_model = new Application_Model_Custom();
//                    $custom_model->getFriendsAndGroups($user_id);
                $model = new Application_Model_Common();

                $friend_list_cond = "fr.user_sent_from = $user_id and fr.user_sent_to != $user_id";
//              $friend_list_records = $model->getAllRecordDetail(TABLE_FRIEND_REQUESTS, $cond, "rand()", "15");
                $friend_list_records = $model->getAllRecordFromLeftJoin(array("fr" => TABLE_FRIEND_REQUESTS), array("u" => TABLE_USERS), "fr.user_sent_to=u.user_id", $friend_list_cond, "rand()", "15");
                $new_friend_array = array();
                foreach ($friend_list_records as $f_key => $f_value)
                {
                    $friend_id = $f_value["user_id"];
                    if (in_array($friend_id, $new_friend_array))
                        unset($friend_list_records[$f_key]);
                    else
                        $new_friend_array[] = $friend_id;
                }

                $country_records = $model->getAllRecordDetail(TABLE_COUNTRY, "1", "country_name");

//                $join_records = $model->getAllRecordFromLeftJoin(array("u" => TABLE_USERS), array("c" => TABLE_COUNTRY), "c.country_id=u.user_country", "u.user_id=$user_id");
//                $record = $join_records[0];
                $record = $model->getdata(TABLE_USERS, "user_id=$user_id");
//                prd($record);
                $rating_record = $custom_model->getRating($user_id);

                $getUserEventsCounts = $custom_model->getUserEventsCounts($user_id);

//                prd($getUserEventsCounts);
                $this->view->totalRating = $rating_record["totalrating"];
                $this->view->first_name = $record["user_first_name"];
                $this->view->last_name = $record["user_last_name"];
                $this->view->user_gender = $record["user_gender"];
                $this->view->user_email = $record["user_email"];
//                $this->view->city = $record["user_city"];
//                $this->view->user_country = $record["user_country"];
                $this->view->user_location = $record["user_location"];
                $this->view->user_bio = $record["user_bio"];
                $this->view->user_education = $record["user_education"];
                $this->view->user_work = $record["user_work"];
                $this->view->username = $record["user_username"];
                $this->view->date_of_birth = $record["user_dob"];
//                $this->view->from_city_country = $record["user_city"] . ", " . $record["country_name"];
                $this->view->published_events = $getUserEventsCounts["published"];
                $this->view->private_events = $getUserEventsCounts["private"];
                $this->view->public_events = $getUserEventsCounts["public"];
                $this->view->tickets_sold = "0";
                $this->view->attended_events = $getUserEventsCounts["attended"];
                $this->view->friendList = $friend_list_records;
//                $this->view->friendList = $custom_model->getFriendsAndGroups($user_id);

                if (empty($record["user_dob"]))
                {
                    $this->view->birth_day = "0";
                    $this->view->birth_month = "0";
                    $this->view->birth_year = "0";
                }
                else
                {
                    $exploded_dob = explode("-", trim($record["user_dob"]));
                    $this->view->birth_day = $exploded_dob[0];
                    $this->view->birth_month = $exploded_dob[1];
                    $this->view->birth_year = $exploded_dob[2];
                }

                $member_image = MEMBER_IMAGE_PATH . "/" . $user_id . ".jpg";
                if (!is_file($member_image))
                    $member_image = MEMBER_NO_IMAGE_FILE;

                $this->view->member_image = getUrl($member_image);
                $this->view->country_records_arr = $country_records;

                $this->view->user_facebook_id = trim($record["user_facebook_id"]);
                $this->view->user_facebook_username = trim($record["facebook_username"]);
                $this->view->user_twitter_id = trim($record["user_twitter_id"]);
                $this->view->user_twitter_username = trim($record["twitter_username"]);
                $this->view->user_linkedin_id = trim($record["user_linkedin_id"]);
                $this->view->user_linkedin_username = trim($record["linkedin_username"]);
                $this->view->user_id = $user_id;

                $created_events = $custom_model->getAllEventDetails("event_created_by=$user_id", "e.event_id DESC");
//                prd($created_events);
//                $joined_events = $model->getAllRecordFromLeftJoin(array("e" => TABLE_EVENTS), array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id", "ej_is_accepted = '1' AND ej.ej_user_id = " . $this->user_id, "ej_id");
                $joined_events = $model->getAdapter()->select()
                                ->from(array("ej" => TABLE_EVENT_JOIN))
//                                ->joinLeft(array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id")
                                ->joinLeft(array("e" => TABLE_EVENTS), "e.event_id=ej.ej_event_id")
                                ->joinLeft(array("c" => TABLE_CATEGORY), "c.category_id=e.event_cat_id")
                                ->joinLeft(array("sc" => TABLE_SUB_CATEGORY), "sc.subcategory_id=e.event_subcat_id")
                                ->where("ej_is_accepted = '1' AND ej.ej_user_id = '" . $this->user_id . "' AND ej_is_invited = '0'")
                                ->order("ej_id DESC")
                                ->query()->fetchAll();

                $bookmarked_events = $model->getAllRecordFromInnerJoin(array("eb" => TABLE_EVENT_BOOKMARK), array("e" => TABLE_EVENTS), "eb.eb_event_id=e.event_id", "eb_user_id = " . $this->user_id . " AND event_ended='0'", "eb_id DESC");

                $this->view->createdEvents = $created_events;
                $this->view->joinedEvents = $joined_events;
                $this->view->bookmarkedEvents = $bookmarked_events;

//                $my_rates_records = $custom_model->getUserEventComments("ecm.ecm_user_id = $user_id", "ecm.ecm_id DESC");
//                prd($my_rates_records);
//                $this->view->myRatesRecords = $my_rates_records;
//
//                $my_reviews_records = "";
//                $this->view->myReviewRecords = $my_reviews_records;

                $this->view->loggedin_full_name = $x->read()->user_first_name . " " . $x->read()->user_last_name;

                $myReviewsList = $custom_model->getMyReviewsList($user_id);
//                prd($myReviewsList);
                $myRatesList = $custom_model->getMyRatesList($user_id);

//                prd($myRatesList);

                $this->view->myReviewRecords = $myReviewsList;
                $this->view->myRatesRecords = $myRatesList;
            }
            else
            {
                $this->_redirect("index");
            }
        }

        public function uploadimageAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $user_id = $this->user_id;
                $arr = $this->getRequest()->getPost();
                if (isset($arr["btn_submit"]))
                {
                    if (isset($_FILES["member_img"]["size"]) && $_FILES["member_img"]["size"] > 0)
                    {
                        $source = $_FILES["member_img"]["tmp_name"];
                        $destination = MEMBER_IMAGE_PATH . "/" . $user_id . ".jpg";
                        $width = MEMBER_IMAGE_WIDTH;
                        $height = MEMBER_IMAGE_HEIGHT;

                        $img = new Application_Plugin_SimpleImage();
                        $img->load($source);
                        if ($height == NULL || empty($height))
                        {
                            $img->resizeToWidth($width);
                        }
                        else
                        {
                            $img->resize($width, $height);
                        }
                        $img->save($destination);
                    }
                }
            }
            $this->redirect("profile");
        }

        public function uploadgalleryprofileimageAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            $image_id = $this->getParam("image_id");
            if (!empty($y) && !empty($image_id))
            {
                $user_id = $this->user_id;
                $source = GALLERY_IMAGE_PATH . "/" . $image_id;
                if (is_file($source))
                {
                    $destination = MEMBER_IMAGE_PATH . "/" . $user_id . ".jpg";

                    if (is_file($destination))
                        @unlink($destination);

                    if (!copy($source, $destination))
                        $_SESSION["messageError"] = "Error uploading image. Please try again";
                    else
                        $_SESSION["messageSuccess"] = "Image uploaded";
                }
            }
            else
                $_SESSION["messageError"] = "Error uploading image. Please try again";

            $this->redirect("profile");
        }

        public function updateprofileAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $user_id = $x->read()->user_id;
                $arr = $this->getRequest()->getPost();
                $model = new Application_Model_Common();
//                prd($arr);

                if (!empty($arr["old_password"]) && !empty($arr["new_password"]) && !empty($arr["confirm_password"]))
                {
                    $change_pass = $this->changepasswordAction();
                    if ($change_pass != "1")
                    {
                        $_SESSION["messageError"] = $change_pass;
                        $this->redirect("profile");
                    }
                }

                $user_email = trim($arr["user_email"]);
                $user_username = trim($arr["user_username"]);

                if (!empty($user_email))
                {
                    $is_exists = $model->is_exists(TABLE_USERS, "user_email='$user_email' and user_id != $user_id");
                    if (!empty($is_exists))
                    {
                        $_SESSION["messageError"] = "Email already exists";
                        $this->redirect("profile");
                    }
                    else
                    {
                        if (!empty($user_username))
                        {
                            $is_username_exists = $model->is_exists(TABLE_USERS, "user_username='$user_username' and user_id != $user_id");
                            if (!empty($is_username_exists))
                            {
                                $_SESSION["messageError"] = "Username already exists";
                                $this->redirect("profile");
                            }
                        }
                    }
                }

                $data_array = array(
                    "user_first_name" => trim($arr["user_first_name"]),
                    "user_last_name" => trim($arr["user_last_name"]),
                    "user_username" => trim($arr["user_username"]),
                    "user_bio" => trim($arr["user_bio"]),
                    "user_gender" => trim($arr["user_gender"]),
                    "user_email" => trim($arr["user_email"]),
//                    "user_city" => trim($arr["user_city"]),
                    "user_education" => trim($arr["user_education"]),
                    "user_work" => trim($arr["user_work"]),
//                    "user_country" => trim($arr["user_country"]),
                    "user_location" => trim($arr["user_location"]),
                    "user_dob" => trim($arr["day"]) . "-" . trim($arr["month"]) . "-" . trim($arr["year"]),
                );
//                prd($data_array);
                $model->updateRecordData($data_array, "user_id=$user_id", TABLE_USERS);
                $_SESSION["messageSuccess"] = "Profile updated";
                $this->redirect("profile");
            }
        }

        public function changepasswordAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            $user_id = $x->read()->user_id;

            if ($this->getRequest()->isPost() && !empty($y))
            {
                $arr = $this->getRequest()->getPost();

                $old_pasword = trim($arr["old_password"]);
                $new_pasword = trim($arr["new_password"]);
                $confirm_pasword = trim($arr["confirm_password"]);

                $enc_old_password = md5($old_pasword);
                $enc_new_password = md5($confirm_pasword);

                if (strcmp($new_pasword, $confirm_pasword) != 0)
                {
                    $returnValue = "New passwords does not match";
                }
                else
                {
                    $model = new Application_Model_Common();
                    $record = $model->getdata(TABLE_USERS, "user_id=$user_id and user_password='$enc_old_password'");
                    if (empty($record))
                    {
                        $returnValue = "Invalid password";
                    }
                    else
                    {
                        $update_data_array = array(
                            "user_password" => $enc_new_password
                        );
                        $model->updateRecordData($update_data_array, "user_id=$user_id", TABLE_USERS);
                        $returnValue = "1";
                    }
                }
                return $returnValue;
            }
        }

        public function publicprofileAction()
        {
            $this->view->bodyClass = 'profile-events';
            $isLoggedIn = "no";
            $isFriend = "no";
            $requestPending = "no";
            $user_id = $this->getParam("user_id");

            $custom_model = new Application_Model_Custom();
            $model = new Application_Model_Common();
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $logged_in_user_id = $x->read()->user_id;
                $isLoggedIn = "yes";

                $whereCond = "user_sent_from = $logged_in_user_id and user_sent_to = $user_id";
                $is_friend = $model->is_exists(TABLE_FRIEND_REQUESTS, $whereCond);
                if (!empty($is_friend))
                {
                    if ($is_friend["is_accepted"] == "1")
                        $isFriend = "yes";
                    else
                        $requestPending = "yes";
                }
            }

            $friend_list_cond = "fr.user_sent_from = $user_id and fr.user_sent_to != $user_id and u.user_active='1'";
//            $friend_list_records = $model->getAllRecordDetail(TABLE_FRIEND_REQUESTS, $cond, "rand()", "15");
            $friend_list_records = $model->getAllRecordFromLeftJoin(array("fr" => TABLE_FRIEND_REQUESTS), array("u" => TABLE_USERS), "fr.user_sent_to=u.user_id", $friend_list_cond, "rand()", "15");
            $country_records = $model->getAllRecordDetail(TABLE_COUNTRY, "1", "country_name");
            $join_records = $model->getAllRecordFromLeftJoin(array("u" => TABLE_USERS), array("c" => TABLE_COUNTRY), "c.country_id=u.user_country", "u.user_id=$user_id and u.user_active='1'");
            $record = $join_records[0];
            $rating_record = $custom_model->getRating($user_id);
//                    prd($requestPending);
            $this->view->totalRating = $rating_record["totalrating"];
            $this->view->isLoggedIn = $isLoggedIn;
            $this->view->logged_in_user_id = $logged_in_user_id;
            $this->view->logged_in_first_name = $x->read()->user_first_name;
            $this->view->logged_in_last_name = $x->read()->user_last_name;
            $this->view->isFriend = $isFriend;
            $this->view->friendList = $friend_list_records;
            $this->view->requestPending = $requestPending;
            $this->view->enc_user_id = getEncryptedString($user_id);
            $this->view->user_id = $user_id;
            $this->view->first_name = $record["user_first_name"];
            $this->view->last_name = $record["user_last_name"];
            $this->view->user_email = $record["user_email"];
            $this->view->city = $record["user_city"];
            $this->view->user_country = $record["user_country"];
            $this->view->user_bio = $record["user_bio"];
            $this->view->username = $record["user_username"];
            $this->view->date_of_birth = $record["user_dob"];
            $this->view->from_city_country = $record["user_city"] . ", " . $record["country_name"];
            $this->view->published_events = "0";
            $this->view->private_events = "0";
            $this->view->public_events = "0";
            $this->view->tickets_sold = "0";
            $this->view->attended_events = "0";

            if (empty($record["user_dob"]))
            {
                $this->view->birth_day = "0";
                $this->view->birth_month = "0";
                $this->view->birth_year = "0";
            }
            else
            {
                $exploded_dob = explode("-", trim($record["user_dob"]));
                $this->view->birth_day = $exploded_dob[0];
                $this->view->birth_month = $exploded_dob[1];
                $this->view->birth_year = $exploded_dob[2];
            }

            $member_image = MEMBER_IMAGE_PATH . "/" . $user_id . ".jpg";
            if (!is_file($member_image))
                $member_image = MEMBER_NO_IMAGE_FILE;

            $created_events = $custom_model->getAllEventDetails("event_created_by=$user_id AND event_active = '1'");
//            $joined_events = $model->getAllRecordFromLeftJoin(array("e" => TABLE_EVENTS), array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id", "ej_is_accepted = '1' AND ej.ej_user_id = " . $user_id, "ej_id");
            $joined_events = $model->getAdapter()->select()
                            ->from(array("e" => TABLE_EVENTS))
                            ->joinLeft(array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id")
                            ->joinLeft(array("c" => TABLE_CATEGORY), "c.category_id=e.event_cat_id")
                            ->joinLeft(array("sc" => TABLE_SUB_CATEGORY), "sc.subcategory_id=e.event_subcat_id")
                            ->where("ej_is_accepted = '1' AND ej.ej_user_id = " . $user_id . " AND event_active='1' AND ej_is_invited = '0'")
                            ->order("ej_id DESC")
                            ->query()->fetchAll();

            $bookmarked_events = $model->getAllRecordDetail(TABLE_EVENT_BOOKMARK, "eb_user_id = " . $user_id);

            $this->view->createdEvents = $created_events;
            $this->view->joinedEvents = $joined_events;
            $this->view->bookmarkedEvents = $bookmarked_events;

            $my_rates_records = $custom_model->getUserEventComments("ecm.ecm_user_id = $user_id", "ecm.ecm_id DESC");
            $this->view->myRatesRecords = $my_rates_records;

            $my_reviews_records = "";
            $this->view->myReviewRecords = $my_reviews_records;

            $this->view->member_image = getUrl($member_image);
            $this->view->country_records_arr = $country_records;
            $cond = "user_rate_from=" . $user_id;
            $order = "rating_id desc";
            $limit = "10";
            $ratingData = $model->getAllRecordDetail(TABLE_RATING, $cond, $order, $limit);
            $ratingAdapter = $model->getAllRecordDetail(TABLE_RATING, $cond, $order, $limit, true);

            $adapter = new Zend_Paginator_Adapter_DbSelect($ratingAdapter);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);

//            gcm($paginator); 
            //prd($paginator);
//            die;
//            $paginator->type_of_data = 'myreview';
            $this->view->paginator1 = $paginator;
            //my rates data
            $mustacheData = array();
            foreach ($ratingData as $rKey => $rValue)
            {
                $mustacheData['myreview'][$rKey]['avatar'] = is_file(ROOT_PATH . "/" . MEMBER_IMAGE_PATH . "/" . $rValue['user_rate_to'] . ".jpg") ? $this->view->baseUrl(MEMBER_IMAGE_PATH . "/" . $rValue['user_rate_to'] . ".jpg") : $this->view->baseUrl(MEMBER_NO_IMAGE_FILE);
                ;
                $mustacheData['myreview'][$rKey]['comments'] = $rValue['comments'];
            }
            $this->view->reviewMustacheData = !empty($mustacheData) ? json_encode($mustacheData) : json_encode(array('noreview' => true));

            $cond = "user_rate_to=" . $user_id;
            $userReviewAdapter = $model->getAllRecordFromInnerJoin(array("ur" => TABLE_RATING), array("u" => TABLE_USERS), 'u.user_id = ur.user_rate_from', $cond, $order, $limit, true);
            $userReviewData = $model->getAllRecordFromInnerJoin(array("ur" => TABLE_RATING), array("u" => TABLE_USERS), 'u.user_id = ur.user_rate_from', $cond, $order, $limit);
            $adapter = new Zend_Paginator_Adapter_DbSelect($userReviewAdapter);
            $paginator = new Zend_Paginator($adapter);

            $paginator->setItemCountPerPage(1);
            $this->view->paginator = $paginator;

            //mustachedata
            $mustacheData = array();
            foreach ($userReviewData as $rKey => $rValue)
            {
                $mustacheData['myreview'][$rKey]['rating'] = $rValue['rating'] . '/' . RATING_STARS_MAX;
                $mustacheData['myreview'][$rKey]['posted_date'] = date('F j Y', strtotime($rValue['rating_timestamp']));
                $mustacheData['myreview'][$rKey]['avatar'] = is_file(ROOT_PATH . "/" . MEMBER_IMAGE_PATH . "/" . $rValue['user_rate_to'] . ".jpg") ? $this->view->baseUrl(MEMBER_IMAGE_PATH . "/" . $rValue['user_rate_to'] . ".jpg") : $this->view->baseUrl(MEMBER_NO_IMAGE_FILE);
                $mustacheData['myreview'][$rKey]['profile_link'] = $this->view->baseUrl("profile/publicprofile/" . $rValue["user_id"]);
                $mustacheData['myreview'][$rKey]['comments'] = $rValue['comments'];
                $mustacheData['myreview'][$rKey]['reviewer_name'] = $rValue['user_first_name'] . " " . $rValue['user_last_name'];
            }
            $this->view->mymustacheData = !empty($mustacheData) ? json_encode($mustacheData) : json_encode(array('noreview' => true));

            //list of reviews data

            if ($this->getRequest()->isPost())
            {

                $data = $this->getRequest()->getPost();

                if (!empty($data['comments']) && $isLoggedIn == 'yes')
                {
                    $data_array = array(
                        'comments' => trim($data["comments"]),
                        'user_rate_from' => $logged_in_user_id,
                        'user_rate_to' => $user_id,
                        'from_ipaddress' => $_SERVER["REMOTE_ADDR"],
                    );

                    $model->insertRecordData($data_array, TABLE_RATING);
                    $_SESSION["messageSuccess"] = "Comment posted";
                }
            }
        }

        public function updatesocialusernameAction()
        {
            $arr = $this->_getAllParams();
//            prd($arr);
            if (isset($arr["social_network"]) && isset($arr["username"]))
            {
                $model = new Application_Model_Common();
                $social_network = trim($arr["social_network"]);
                $username = trim($arr["username"]);
                $user_id = $this->user_id;

                $whereCond = 'user_id != ' . $user_id . ' and ' . $social_network . '_username = "' . $username . '"';
                $is_exists = $model->is_exists(TABLE_USERS, $whereCond);
                if (empty($is_exists))
                {
                    $data_array = array(
                        $social_network . "_username" => $username
                    );
                    $model->updateRecordData($data_array, "user_id=$user_id", TABLE_USERS);
                    $_SESSION["messageSuccess"] = ucwords($social_network) . " username updated";
                }
                else
                {
                    $_SESSION["messageError"] = ucwords($social_network) . " username already exists";
                }
            }
            $this->redirect("profile");
        }

        public function orderbyAction()
        {
            $this->view->bodyClass = 'profile-events';
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $user_id = $x->read()->user_id;
                $custom_model = new Application_Model_Custom();
//                    $custom_model->getFriendsAndGroups($user_id);
                $model = new Application_Model_Common();

                $friend_list_cond = "fr.user_sent_from = $user_id and fr.user_sent_to != $user_id";
//              $friend_list_records = $model->getAllRecordDetail(TABLE_FRIEND_REQUESTS, $cond, "rand()", "15");
                $friend_list_records = $model->getAllRecordFromLeftJoin(array("fr" => TABLE_FRIEND_REQUESTS), array("u" => TABLE_USERS), "fr.user_sent_to=u.user_id", $friend_list_cond, "rand()", "15");
                $new_friend_array = array();
                foreach ($friend_list_records as $f_key => $f_value)
                {
                    $friend_id = $f_value["user_id"];
                    if (in_array($friend_id, $new_friend_array))
                        unset($friend_list_records[$f_key]);
                    else
                        $new_friend_array[] = $friend_id;
                }

                $country_records = $model->getAllRecordDetail(TABLE_COUNTRY, "1", "country_name");

                $join_records = $model->getAllRecordFromLeftJoin(array("u" => TABLE_USERS), array("c" => TABLE_COUNTRY), "c.country_id=u.user_country", "u.user_id=$user_id");
                $record = $join_records[0];
                $rating_record = $custom_model->getRating($user_id);

                $getUserEventsCounts = $custom_model->getUserEventsCounts($user_id);

//                prd($getUserEventsCounts);
                $this->view->totalRating = $rating_record["totalrating"];
                $this->view->first_name = $record["user_first_name"];
                $this->view->last_name = $record["user_last_name"];
                $this->view->user_gender = $record["user_gender"];
                $this->view->user_email = $record["user_email"];
                $this->view->city = $record["user_city"];
                $this->view->user_country = $record["user_country"];
                $this->view->user_bio = $record["user_bio"];
                $this->view->user_education = $record["user_education"];
                $this->view->user_work = $record["user_work"];
                $this->view->username = $record["user_username"];
                $this->view->date_of_birth = $record["user_dob"];
                $this->view->from_city_country = $record["user_city"] . ", " . $record["country_name"];
                $this->view->published_events = $getUserEventsCounts["published"];
                $this->view->private_events = $getUserEventsCounts["private"];
                $this->view->public_events = $getUserEventsCounts["public"];
                $this->view->tickets_sold = "0";
                $this->view->attended_events = $getUserEventsCounts["attended"];
                $this->view->friendList = $friend_list_records;

                if (empty($record["user_dob"]))
                {
                    $this->view->birth_day = "0";
                    $this->view->birth_month = "0";
                    $this->view->birth_year = "0";
                }
                else
                {
                    $exploded_dob = explode("-", trim($record["user_dob"]));
                    $this->view->birth_day = $exploded_dob[0];
                    $this->view->birth_month = $exploded_dob[1];
                    $this->view->birth_year = $exploded_dob[2];
                }

                $member_image = MEMBER_IMAGE_PATH . "/" . $user_id . ".jpg";
                if (!is_file($member_image))
                    $member_image = MEMBER_NO_IMAGE_FILE;

                $this->view->member_image = getUrl($member_image);
                $this->view->country_records_arr = $country_records;

                $this->view->user_facebook_id = trim($record["user_facebook_id"]);
                $this->view->user_facebook_username = trim($record["facebook_username"]);
                $this->view->user_twitter_id = trim($record["user_twitter_id"]);
                $this->view->user_twitter_username = trim($record["twitter_username"]);
                $this->view->user_linkedin_id = trim($record["user_linkedin_id"]);
                $this->view->user_linkedin_username = trim($record["linkedin_username"]);
                $this->view->user_id = $user_id;

                $get_event_type = $this->getParam("get_event_type");
                $get_sort_by = $this->getParam("sort_by");
                $this->view->tabClass = $get_event_type;
                $this->view->mainTabClass = "events";

                $created_condition = "event_created_by=$user_id";
                $created_order = "event_id DESC";
                $joined_condition = "ej_is_accepted = '1' AND ej_is_invited = '0' AND ej.ej_user_id = " . $this->user_id;
                $joined_order = "ej_id DESC";
                $bookmarked_condition = "eb_user_id = " . $this->user_id;
                $bookmarked_order = "eb_id DESC";

                switch ($get_event_type)
                {
                    case "created":
                        {
//                            $created_condition = "";
                            if ($get_sort_by == "group_size")
                                $created_order = "event_group_size ASC";
                            else if ($get_sort_by == "date")
                                $created_order = "event_id ASC";
                            break;
                        }
                    case "joined":
                        {
//                            $joined_condition = "";
                            if ($get_sort_by == "group_size")
                                $created_order = "event_group_size ASC";
                            else if ($get_sort_by == "date")
                                $joined_order = "ej_id ASC";
                            break;
                        }
                    case "bookmarked":
                        {
//                            $bookmarked_condition = "";
                            if ($get_sort_by == "group_size")
                                $created_order = "event_group_size ASC";
                            else if ($get_sort_by == "date")
                                $bookmarked_order = "eb_id ASC";
                            break;
                        }
                }

                $created_events = $custom_model->getAllEventDetails($created_condition, $created_order);

//                $joined_events = $model->getAllRecordFromLeftJoin(array("e" => TABLE_EVENTS), array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id", "ej_is_accepted = '1' AND ej.ej_user_id = " . $this->user_id, "ej_id");
                $joined_events = $model->getAdapter()->select()
                                ->from(array("e" => TABLE_EVENTS))
                                ->joinLeft(array("ej" => TABLE_EVENT_JOIN), "e.event_id=ej.ej_event_id")
                                ->joinLeft(array("c" => TABLE_CATEGORY), "c.category_id=e.event_cat_id")
                                ->joinLeft(array("sc" => TABLE_SUB_CATEGORY), "sc.subcategory_id=e.event_subcat_id")
                                ->where($joined_condition)
                                ->order($joined_order)
                                ->query()->fetchAll();

                $bookmarked_events = $model->getAllRecordFromInnerJoin(array("eb" => TABLE_EVENT_BOOKMARK), array("e" => TABLE_EVENTS), "eb.eb_event_id=e.event_id", $bookmarked_condition, $bookmarked_order);

                $this->view->createdEvents = $created_events;
                $this->view->joinedEvents = $joined_events;
                $this->view->bookmarkedEvents = $bookmarked_events;

                $my_rates_records = $custom_model->getUserEventComments("ecm.ecm_user_id = $user_id", "ecm.ecm_id DESC");
                $this->view->myRatesRecords = $my_rates_records;

                $my_reviews_records = "";
                $this->view->myReviewRecords = $my_reviews_records;

                $this->view->loggedin_full_name = $x->read()->user_first_name . " " . $x->read()->user_last_name;
                $this->renderScript("profile/index.phtml");
            }
            else
            {
                $this->_redirect("index");
            }
        }

        public function addeventcommentAction()
        {
            if (isset($this->user_id) && $this->getRequest()->isPost())
            {
                $user_id = $this->user_id;
                $arr = $this->getRequest()->getPost();

                $ratings = $arr["ratings"];
                $comment = addslashes($arr["comment"]);
                $event_id = $arr["event_id"];

                $model = new Application_Model_Common();

                $data_array = array(
                    "ecm_event_id" => $event_id,
                    "ecm_user_id" => $user_id,
                    "ecm_ratings" => $ratings,
                    "ecm_comment" => $comment,
                    "from_ipaddress" => $_SERVER["REMOTE_ADDR"]
                );

                $whereCond = "ecm_event_id = '$event_id' AND ecm_user_id = '$user_id' AND ecm_ratings = '$ratings' AND ecm_comment = '$comment'";
                $is_exists = $model->is_exists(TABLE_EVENT_COMMENTS, $whereCond);
                if (empty($is_exists))
                {
                    $model->insertRecordData($data_array, TABLE_EVENT_COMMENTS);

                    $event_record = $model->getdata(TABLE_EVENTS, "event_id = $event_id");
                    $this->appendcommentajax($event_record["event_created_by"], $ratings, $comment);
                }

                die;
            }
        }

        public function appendcommentajax($event_created_by, $rating, $comment)
        {
            $str = '<li>
                    <img src="' . getUserImage($event_created_by) . '" alt="' . $this->loggedin_full_name . '" class="fl" style="width:32px;height:32px;"/>
                    <div class="details fl">
                        <div class="e-title">
                            <a class="p-name tlink" href="#">' . $this->loggedin_full_name . ',</a>
                        </div>
                        <div class="sr">
                            <span class="fl">RATE:&nbsp;&nbsp;&nbsp;</span> 
                            <ul class="profile-stars li-border-padding-none">';

            $li_classname = "";
            $test_i = 1;
            $grey_start_count = 10;
            if ($rating > 0)
            {
                $li_classname = "";
                $yellow_star_count = $rating;
                $grey_start_count = RATING_STARS_MAX - $yellow_star_count;
            }

            for ($yellow_i = $yellow_star_count; $yellow_i >= "1"; $yellow_i--)
            {
                $str .= '<li class="' . $li_classname . '"><img src="' . IMAGES_PATH . '/star-yellow.png" alt="stars" rel="' . $test_i . '"/></li>';
                $test_i++;
            }
            for ($grey_i = "1"; $grey_i <= $grey_start_count; $grey_i++)
            {
                $str .= '<li class="' . $li_classname . '"><img src="' . IMAGES_PATH . '/star-grey.png" alt="stars" rel="' . $test_i . '"/></li>';
                $test_i++;
            }

            $str .= '</ul>
                                                                                    <div class="cf"></div>
                                                                                </div>
                                                                                <p>' . $comment . '</p>
                                                                            </div>
                                                                            <div class="cf"></div>
                                                                        </li>';

            echo $str;
        }

    }

    